<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/12/26
 * Time: 13:23
 */
namespace app\admin\controller;
use app\common\lib\db\DbCommon;
use app\common\lib\exception\ApiException;
use think\Db;
use think\Model;
use think\Request;
class Member extends Base{
    public function index(){
        if (request()->isAjax()) {
            extract(input()); //将接收的参数赋值给变量
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $map = array();
            //用户模糊昵称
            if(isset($keyword) && $keyword!="")
            {
                $map['user_username|user_phone'] = ['like',"%" . $keyword . "%"];
            }
            //用户类别
            if(isset($is_vip) && $is_vip!="")
            {
                $map['is_vip'] = $is_vip;
            }
            //用户状态
            if(isset($user_status) && $user_status!="")
            {
                $map['user_status'] = $user_status;
            }
            //用户性别
            if(isset($user_gender) && $user_gender!="")
            {
                $map['user_gender'] = $user_gender;
            }
            try{
                $count = Db::name('u_user')->where($map)->count();  //总条目数据
                $count2 = Db::name('u_user')
                    ->where($map)
                    ->column('user_id');// 获取总条数
                $lists = Db::name('u_user')->page($NowPage,$limits)->where($map)->select();  //当前页
            }catch (\Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage()]);
            }
            $str = '';
            if($count2){
                $str = implode($count2,',');
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists,'count2'=>$str]);
        }
        return $this->fetch();
    }
    public function add(){
        if(Request::instance()->isPost()){
            $vipTime =input('vipTime');
            $id= input('id');
            $data['end_time'] = $vipTime;
            $data['is_vip'] = 2;
            $res = Db::table('up_u_user')->where('user_id',$id)->update($data);
//            try{
//                $res = Db::table('up_u_user')->where('user_id',$id)->update($data);
//            }catch (\Exception $e){
//                return show('200','ok',$e->getMessage());
//            }
            return show('200','已更新',$res);

        }
        $user_id = input('param.user_id');
        $info = Db::table('up_u_user')->where('user_id',$user_id)->find();
        $this->assign('user_id',$user_id);
        $this->assign('info',$info);
        return $this->fetch();
    }

    /**
     * 导出
     * @return \think\response\Json
     */
    public function outExcel(){
        $order_ids = input('ids');
        if($order_ids == ''){
            return json(['code' => 100, 'msg' => '请选择要操作的用户']);
        }
        $lists = Db::table('up_u_user')
            ->where('user_id', 'IN', $order_ids)
            ->field('user_id,user_username,user_phone,user_nickname,user_status,user_age,user_birthday,user_gender,is_vip,user_time')
            ->order('user_time desc')
            ->select();
        foreach ($lists as $key=>&$vo){
            if($vo['user_status']==1){
                $vo['status'] = '正常';
            }elseif ($vo['user_status']==2){
                $vo['status'] = '拒绝';
            }elseif ($vo['user_status']==3){
                $vo['status'] = '冻结';
            }else{
             $vo['status'] = '已删除';
            }
            if($vo['user_gender']==1){
                $vo['sex'] = '男';
            }else{
                $vo['sex'] = '女';
            }
            if($vo['is_vip']==1){
                $vo['vip'] = '普通用户';
            }else{
                $vo['vip'] = '女';
            }
            unset($lists[$key]['user_status']);
            unset($lists[$key]['user_gender']);
            unset($lists[$key]['is_vip']);
        }
        $cellname = [
            ['user_id','用户ID',10,'LEFT'],
            ['user_username','用户名',20,'LEFT'],
            ['user_phone','手机号',20,'LEFT'],
            ['user_nickname','用户昵称',20,'LEFT'],
            ['user_age','年龄',20,'LEFT'],
            ['user_birthday','出生日期',20,'LEFT'],
            ['user_time','注册时间',20,'LEFT'],
            ['status','用户状态',20,'LEFT'],
            ['sex','性别',20,'LEFT'],
            ['vip','是否VIP',20,'LEFT'],
        ];
        $res = exportExcel('用户列表','member',$cellname,$lists);
        return json($res);

    }
    public function update()
    {
        if(\request()->isPOST()){
            $data = input('post.');
            $res = Model('UUser')->update_user_msg($data['user_id'],$data);
            if(!$res) {
                throw new ApiException('没有做任何更改','200');
            }
            return show('200','已更新',$res);
        }else {
            $user_id = Request::instance()->param('user_id');
            $data = Db::name('u_user')->where(['user_id' => $user_id])->find();
            $area = DbCommon::area($data['user_sheng'],$data['user_shi'],$data['user_qu']);
            $this->assign([
                'data' => $data,
                'area' => $area
            ]);
            return $this->fetch();
        }
    }

    /**
     * Ajax抛送删除
     * @return \think\response\Json
     */
    public function del()
    {
        $user_id = Request::instance()->param('id');
        $is_del = $this->is_del($user_id);
        if($is_del) {
            return json(['code'=>200,'msg'=>'当前用户已删除']);
        }
        $res = Model('UUser')->save(['user_status'=>'4'],['user_id'=>$user_id]);
        if(!$res) {
            return json(['code'=>500,'msg'=>'数据库错误']);
        }
        return json(['code'=>200,'msg'=>'已删除']);
    }

    /**
     * 查看是否已经删除，而不是删除继续点击删除
     * @param number $user_id 用户id
     * @return bool $is_del
     */
    public function is_del($user_id)
    {
        $res = Model('UUser')->get(['user_id' => $user_id,'user_status' => '4']);
        if(!$res) {
            return false;
        }
        return true;
    }

    /**
     * @param number $start_time
     * @param number $end_time
     * @param number $month_time
     * @return bool 升级结果
     */
    public function up_vip()
    {
        if(\request()->isPost()){
            $data = input('post.');
            $is_vip = $this->is_vip($data['user_id']);
            if($is_vip) {
                return show('200','当前用户已是vip，无需升级',[]);
            }
            $vip_info = array();
            $vip_info['start_time'] = time();
            $vip_info['end_time'] = $vip_info['start_time'] + $data['month_time'] * 24 * 60 * 60 * 30;
            $vip_info['is_vip'] = 2;
            $vip_info['start_time'] = date("Y-m-d H:i:s", $vip_info['start_time']);
            $vip_info['end_time'] = date("Y-m-d H:i:s", $vip_info['end_time']);
            try{
                $up_vip = Model('UUser')->save($vip_info,['user_id'=>$data['user_id']]);
            }catch (\Exception $e){
                return show('200','ok',$e->getMessage());
            }
            if(!$up_vip) {
                return show('200','数据库错误',[]);
            }
            return show('200','已升级成'.$data['month_time'].'月会员',[]);
        }else {
            $user_id = Request::instance()->param('user_id');
            $data = Db::name('u_user')->where(['user_id' => $user_id])->find();
            $this->assign(['data'=>$data]);
            return $this->fetch();
        }
    }

    /**
     * 当前用户是否是会员
     * @param number $user_id
     * @return bool|string
     */
    public function is_vip($user_id)
    {
        if(!isset($user_id) || empty($user_id) ||!is_numeric($user_id)){
            return show('200','参数不合法',[]);
        }
        $ret = Model('UUser')->get(['user_id' => $user_id,'is_vip'=>'2']);
        if(!$ret) {
            return false;
        }
        return true;
    }
}